import FormModalBtn from '@/components/FormModalBtn';
import MyTable from '@/components/MyTable';
import { findByAgentBySelectList } from '@/services/agent';
import { getResult } from '@/services/sys/attribute';
import { findByWalletBackLogList1 } from '@/services/sys/log';
import { editIntegralReChange, editWalletReChange } from '@/services/users';
import { ActionType } from '@ant-design/pro-table/es/typing';
import { Link } from '@umijs/max';
import React, { useRef } from 'react';

const getAttr = async (key: string) => {
  return await getResult(key, 1);
};

export const Index: React.FC = () => {
  // @ts-ignore
  const actionRef = useRef<ActionType>();
  //userstatus
  const columns = [
    {
      title: '编号',
      dataIndex: 'id',
      search: false,
      key: 'id',
    },

    {
      title: '头像',
      dataIndex: 'avatar',
      valueType: 'avatar',
      key: 'avatar',
      search: false,
    },
    {
      title: '姓名',
      dataIndex: 'name',
      key: 'name',
      render: (
        _: any,
        row: API.AgentInvitationRelationship,
        index: number,
        action: any,
      ) => {
        return (
          <Link target="_blank" to={'/users/tree?userId=' + row?.id}>
            {_}
          </Link>
        );
      },
    },

    {
      title: '手机号',
      dataIndex: 'phone',
      key: 'phone',
    },
    {
      title: '邀请人',
      dataIndex: 'parentName',
      key: 'parentName',
    },
    {
      title: '代理等级',
      dataIndex: 'agentId',
      key: 'agentId',
      width: 'md',
      valueType: 'select',
      request: findByAgentBySelectList,
    },
    {
      title: '余额',
      key: 'balance',
      dataIndex: 'balance',
      valueType: 'money',
      search: false,
      sorter: true,
    },
    {
      title: '积分',
      key: 'integral',
      dataIndex: 'integral',
      search: false,
    },

    {
      title: '时间',
      dataIndex: 'createDate',
      valueType: 'dateTime',
      hideInSearch: true,
      key: 'createDate',
    },
    {
      title: '操作',
      width: '164px',
      key: 'option',
      valueType: 'option',
      render: (
        _: any,
        row: API.AgentInvitationRelationship,
        index: number,
        action: any,
      ) => [
        <FormModalBtn
          initialValues={{ name: row?.name, userId: row?.id }}
          title="财务"
          btnText="财务"
          columns={[
            {
              title: 'userId',
              dataIndex: 'userId',
              key: 'userId',
              width: 'md',
              readonly: true,
              formItemProps: {
                hidden: true,
              },
            },
            {
              title: '姓名',
              dataIndex: 'name',
              key: 'name',
              width: 'md',
              readonly: true,
            },
            {
              title: '财务动作',
              dataIndex: 'state',
              key: 'state',
              width: 'md',
              valueType: 'select',
              request: () => {
                return getAttr('usersblancewithdrawaltype');
              },
              formItemProps: {
                debounceTime: 300,
                rules: [
                  {
                    required: true,
                  },
                ],
              },
            },
            {
              title: '金额',
              dataIndex: 'price',
              key: 'price',
              width: 'md',
              valueType: 'money',
              fieldProps: {
                help: 'gggggggggggggggggggggggg',
              },
              formItemProps: {
                rules: [
                  {
                    required: true,
                  },
                ],
              },
            },
            {
              title: '处理类型',
              dataIndex: 'type',
              key: 'type',
              width: 'md',
              valueType: 'select',
              request: () => {
                return getResult('userswithdrawaltype', 1);
              },
              formItemProps: {
                debounceTime: 300,
                rules: [
                  {
                    required: true,
                  },
                ],
              },
            },
            {
              title: '备注',
              dataIndex: 'remark',
              key: 'remark',
              width: 'md',
              valueType: 'textarea',
              formItemProps: {
                rules: [
                  {
                    required: true,
                  },
                ],
              },
            },
          ]}
          request={editWalletReChange}
          onChange={() => {
            // refresh()
            actionRef.current?.reload();
          }}
        />,
        <FormModalBtn
          initialValues={{ name: row?.name, userId: row?.id }}
          title="积分"
          btnText="积分"
          columns={[
            {
              title: 'userId',
              dataIndex: 'userId',
              key: 'userId',
              width: 'md',
              readonly: true,
              formItemProps: {
                hidden: true,
              },
            },
            {
              title: '姓名',
              dataIndex: 'name',
              key: 'name',
              width: 'md',
              readonly: true,
            },
            {
              title: '积分动作',
              dataIndex: 'state',
              key: 'state',
              width: 'md',
              valueType: 'select',
              valueEnum: getResult('usersblancewithdrawaltype'),
              formItemProps: {
                debounceTime: 300,
                rules: [
                  {
                    required: true,
                  },
                ],
              },
            },
            {
              title: '积分',
              dataIndex: 'integral',
              key: 'integral',
              width: 'md',
              valueType: 'number',
              formItemProps: {
                rules: [
                  {
                    required: true,
                  },
                ],
              },
            },
            {
              title: '备注',
              dataIndex: 'remark',
              key: 'remark',
              width: 'md',
              valueType: 'textarea',
              formItemProps: {
                rules: [
                  {
                    required: true,
                  },
                ],
              },
            },
          ]}
          request={editIntegralReChange}
          onChange={() => {
            //  refresh()
            actionRef.current?.reload();
          }}
        />,
      ],
    },
  ];
  return (
    <MyTable
      actionRef={actionRef}
      search={true}
      request={findByWalletBackLogList1}
      columns={columns}
    />
  );
};
export default Index;
